﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.IO;
using Aim2.Music;

namespace Aim2.Screen {
    class LevelScreen : GameScreen {
        Level level;
        HUD HUD;
        public DirectoryInfo trackFolder { get; set; }
        public Track CurrentTrack { get; set; }
        Vector2 exitingTextOrigin;
        const string exitingText = "EXITING";
        Vector2 exitingTextSize;
        Vector2 exitingTextPosition;
        Color exitingTextColor;
        float exitingTextScale;
        int score;

        public static readonly float maxCount = 1;
        float count;
        Vector2 positionNumber;
        Color colorNumber;

        bool trackFromMenuPlaying = true;

        string[] numbers = new string[4] { " 3 ", " 2 ", " 1 ", "GO!" };

        bool playing;

        float elapsedToEscape;
        const float TimeToEscape = 2f;

        public LevelScreen() {
            level = new Level();
            HUD = new HUD();
            count = 0;
            elapsedToEscape = 0f;
        }

        public override void HandleInput(InputState input) {
            level.HandleInput(input);
        }

        public override void LoadContent() {
            level.Load(ScreenManager.Content, ScreenManager.GraphicsDevice.Viewport);
            HUD.Load(ScreenManager.Content, ScreenManager.GraphicsDevice.Viewport);

            HUD.ScreenManager = ScreenManager;

            CurrentTrack = new Track(AudioManager.CustomTrack, trackFolder);
            BackgroundScreen.Background.TimeBetweenBeats = CurrentTrack.TotalNeeded;
            AudioManager.LoadCustomTrack(trackFolder);
            BackgroundScreen.Background.Bars.NumberOfBarsLayers = 4;
            BackgroundScreen.Background.TimeBetweenBeats = CurrentTrack.TotalNeeded;


            playing = false;

            positionNumber = new Vector2(0, ScreenManager.GraphicsDevice.Viewport.Height / 4f);
            
            colorNumber = new Color(0, 200, 0);

            exitingTextOrigin = new Vector2(0, ScreenManager.ScoreFont.LineSpacing / 2);
            
            exitingTextColor = Color.DarkRed;

            base.LoadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen) {
            base.Update(gameTime, otherScreenHasFocus, false);

            if (Keyboard.GetState().IsKeyDown(Keys.Escape)) {
                elapsedToEscape += (float) gameTime.ElapsedGameTime.TotalSeconds;
                exitingTextScale = 2.0f;
                float pulsate = (float)Math.Sin(elapsedToEscape * 10f) + 1f;

                exitingTextScale += pulsate * 0.5f;

                exitingTextSize = ScreenManager.ScoreFont.MeasureString(exitingText);
                exitingTextPosition = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2 - exitingTextSize.X / 2f * exitingTextScale, ScreenManager.GraphicsDevice.Viewport.Height / 3);

                if (elapsedToEscape >= TimeToEscape)
                    GoBackToMenu();
            }
            else
                elapsedToEscape = 0f;

            if (trackFromMenuPlaying)
                trackFromMenuPlaying = !AudioManager.StopPlayingTrack();


            level.Update((float) gameTime.ElapsedGameTime.TotalSeconds);
            HUD.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

            if (count >= maxCount)
                CurrentTrack.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

            if (level.Player != null)
                score = level.Player.Score;

            CollisionManager.Instance.BuildCollisions();
            CollisionManager.Instance.ApplyCollisions();
        }

        private void GoBackToMenu() {
            ScreenManager.RemoveScreen(this);
            MainMenuScreen menu = new MainMenuScreen();
            AudioManager.PlaySoundEffect("swoosh");
            ScreenManager.AddScreen(menu);
        }

        public override void Draw(GameTime gameTime) {

            ScreenManager.SpriteBatch.Begin();
            level.Draw(ScreenManager.SpriteBatch);
            HUD.Draw(ScreenManager.SpriteBatch, score);

            count += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (count < maxCount) {
                colorNumber.R+=2;
                colorNumber.G--;
                colorNumber.B += 7;
                Vector2 size = ScreenManager.CounterFont.MeasureString(numbers[(int)count]);

                positionNumber.X = ScreenManager.GraphicsDevice.Viewport.Width / 2f - size.X / 2f;

                ScreenManager.SpriteBatch.DrawString(ScreenManager.CounterFont, numbers[(int)(count * 4)],
                                                         positionNumber, colorNumber, 0, Vector2.Zero, 1,
                                                         SpriteEffects.None, 0);
            }
            else {
                if (!playing) {
                    PlayMusic();
                }
                CurrentTrack.Draw(ScreenManager.SpriteBatch);

                Vector2 size = ScreenManager.MenuFont.MeasureString(CurrentTrack.MeasureCount.ToString());

                ScreenManager.SpriteBatch.DrawString(   ScreenManager.MenuFont, 
                                                        CurrentTrack.MeasureCount.ToString(),
                                                         new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2f - size.X / 2f, ScreenManager.GraphicsDevice.Viewport.Height - size.Y), 
                                                         Color.Azure, 
                                                         0,
                                                         Vector2.Zero, 
                                                         1,
                                                         SpriteEffects.None, 0);

            }

            if (elapsedToEscape > 0f) {

                ScreenManager.SpriteBatch.DrawString(ScreenManager.ScoreFont, "EXITING",
                                                     exitingTextPosition, exitingTextColor, 0, exitingTextOrigin, exitingTextScale,
                                                     SpriteEffects.None, 0);
            }

            ScreenManager.SpriteBatch.End();
        }

        public void PlayMusic() {
            AudioManager.PlayCustomSoundTrack();
            playing = true;
        }
    }
}
